﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BLL;
using Model;
using System.Data.SqlClient;
namespace UI
{
    public partial class 主界面 : Form
    {
        private string p;
        private Model_ConsumerBill m;

        public 主界面()
        {
            InitializeComponent();
        }

        public 主界面(string p)
        {
            // TODO: Complete member initialization
            this.p = p;
            InitializeComponent();
        }


        private void toolStripSplitButton1_ButtonClick(object sender, EventArgs e)
        {

        }

        private void 主界面_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("你确定要退出星星餐饮系统吗？", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString() == "No")
            {
              
            }
            else
            {
                Application.ExitThread();
            }
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            系统设置 f = new 系统设置();
            f.Show();
        }

        public void FJfuzhi(ModelRoomType m) {
            label19.Text = m.RTConsume.ToString();

            //MessageBox.Show(m.RTIsDisCount.ToString());
            if (m.RTIsDisCount)
            {
                label18.Text = "享受折扣";
            }
            else
            {
                label18.Text = "不享受折扣";
            }
        }


        //房间
        public void FJ() {
            tabControl2.TabPages.Clear();
            List<ModelRoomType> list = BLLRoomType.BaoXiangLX();
            foreach (ModelRoomType item in list)
            {
                TabPage T = new TabPage(item.RTName);
                T.Tag = item;
                tabControl2.TabPages.Add(T);


                ListView lv = new ListView();
                lv.Dock = DockStyle.Fill;//填充。
                lv.View = View.LargeIcon;//视图模式。
                //以大图标显示使用
                lv.LargeImageList = imageList1;//绑定图片集控件
                //以小图标显示使用
                lv.SmallImageList = imageList2;//绑定图片集控件
                //将ListView添加到TabPage中
                T.Controls.Add(lv);
                lv.SelectedIndexChanged += new EventHandler(li_SelectedIndexChanged);
            }
            Zz();
        }
        //座子
        public void Zz() {
            foreach (TabPage tab in tabControl2.TabPages)
            {

                ModelRoomType mr = (ModelRoomType)tab.Tag;
                Model_Tables mt = new Model_Tables();
                mt.RTID = mr.RTID;
                DataTable dr =BLL_Tables.CXZuoZi(mt);
                ListView lv = (ListView)tab.Controls[0];
                lv.Items.Clear();
                foreach (DataRow item in dr.Rows)
	{
		  ListViewItem li = new ListViewItem();
                    li.Text = item["TableName"].ToString();
                    int TableState = int.Parse(item["TableState"].ToString());
                   
                    li.ImageIndex = TableState;
                    li.Tag = item;
                    lv.Items.Add(li);       
                    
	}


              
               
            }
            
           
        }
        void li_SelectedIndexChanged(object sender, EventArgs e)
        {
            XiaoFxx();
        }
        string CBDiscount;
        string CBID;
        string VipID;
        //点击正在使用的座子获取详细信息
        public void XiaoFxx() {

            //获取tabControl2控件的选项卡中的第一个控件
            ListView lv = (ListView)tabControl2.SelectedTab.Controls[0];
            
            
          
             if (lv.SelectedItems.Count <= 0)
            {

                return;
            }
             //因为之前隐藏值是SqlDataReader所以强转
                 DataRow dr = (DataRow)lv.SelectedItems[0].Tag;


                 //获取餐桌编号
                 string tableID = dr["TableID"].ToString();
                 //获取餐桌状态
                 string TableState = dr["TableState"].ToString();
                    //餐桌名称
                 string TableName = dr["TableName"].ToString();
                // MessageBox.Show(TableName);
                    //房间类型
                 string RTID = dr["RTID"].ToString();
                    //所在区域
                 string TableArea = dr["TableArea"].ToString();
                 Model_Tables mt = new Model_Tables(int.Parse(tableID), TableName,int.Parse(RTID),TableArea,int.Parse(TableState));

                 switch (TableState)
                 {
                     case("0"):
                         label8.Text = "可用";
                         break;
                     case ("1"):
                         label8.Text = "占用";
                         break;
                     case ("2"):
                         label8.Text = "预订";
                         break;
                     case ("3"):
                         label8.Text = "停用";
                         break;
                     default:
                         break;
                 }
            //获取当前桌子消费单号    
                 SqlDataReader drCXDH =BLL_ConsumerBill.CXDH(tableID);
                 while (drCXDH.Read())
                 {
                     CBID = drCXDH["CBID"].ToString();
                     CBDiscount = drCXDH["CBDiscount"].ToString();
                     VipID = drCXDH["VipID"].ToString();
                 }
                 drCXDH.Close();

                 DataTable dt = BLL_ConsumerBill.XiaofeiQK(tableID);

                 double xiaofei = 0;
                 foreach (DataRow  item in dt.Rows)
                 {
                     //获取消费金额
                   string CDMoney = item["CDMoney"].ToString();
                  // MessageBox.Show(item["CBID"].ToString());
                  
                     
                //   MessageBox.Show(CDMoney);
                   xiaofei +=double.Parse(CDMoney);
                   label15.Text = xiaofei.ToString();
                 }
                 //给黑道控件赋值
                 dataGridView1.DataSource = dt;

                  double X=double.Parse(BLL_ConsumerBill.ShangZhuoS(tableID).ToString());
          double  Y=double.Parse(BLL_ConsumerBill.SY_ShangZhuoS().ToString());
                 double A = X /Y*100 ;

                 try
                 {
                     //如果A是0直接报错 
                     int index = A.ToString().IndexOf(".");
                    // MessageBox.Show(A.ToString());
                    //MessageBox.Show(index.ToString());
                     //获取小数点前面的数
                     string str1 = A.ToString().Substring(0, index);
                     //获取小数点后面的两位数
                     string str2 = A.ToString().Substring(index, index + 2);
                     if (str1.Length == 1)
                     {
                         label14.Text = "0" + str1 + "." + str2 + "%";
                     }
                     else
                     {
                         label14.Text = str1 + "." + str2 + "%";
                     }
                 }
                 catch (Exception)
                 {
                     
                   label14.Text = 0 + "%";
                 }
               
               
               
           
        }

        public void SXXXK() {
            //获取选中的选项卡的隐藏值
            ModelRoomType mr = (ModelRoomType)tabControl2.SelectedTab.Tag;
            FJfuzhi(mr);

            label9.Text = BLL_Tables.ZhuoZiCOUNT().ToString();
            label10.Text = BLL_Tables.ZhuoZi_ZY_S().ToString();
            label11.Text = BLL_Tables.ZhuoZi_KY_S().ToString();
            label12.Text = BLL_Tables.ZhuoZi_YD_S().ToString();
            label13.Text = BLL_Tables.ZhuoZi_TY_S().ToString();
        }

       

        private void 主界面_Load(object sender, EventArgs e)
        {
            FJ();

            SXXXK();

           

            //改变颜色
            toolStrip1.BackColor = Color.CadetBlue;


            //设置当前登录
            //label30.Text = ModelAdmins.UserName;
            ////设置当前时间
            //label29.Text = DateTime.Now.ToString();
            //设置定时器的时间(1000毫米代表1秒)
            //timer1.Interval = 1000;
            //打开定时器
            //timer1.Start();
           
        }
        //在选择选项卡时发生事件
        private void tabControl2_Selecting(object sender, TabControlCancelEventArgs e)
        {
            try
            {
                //获取选中的选项卡的隐藏值
                ModelRoomType mr = (ModelRoomType)tabControl2.SelectedTab.Tag;
                //MessageBox.Show(mr.ToString());
                FJfuzhi(mr);
            }
            catch (Exception)
            {
                
               
            }
         
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {

        }

        private void 大图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           ListView LI= (ListView)tabControl2.SelectedTab.Controls[0];
           LI.View = View.LargeIcon;

           if (LI.LargeImageList == null)
           {
               LI.LargeImageList = imageList1;
           }
          
          
           this.toolStripLabel2.Text = "大图标";
        }

        private void 小图标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListView LI = (ListView)tabControl2.SelectedTab.Controls[0];
            LI.View = View.SmallIcon;
            if (LI.SmallImageList==null)
            {
                LI.SmallImageList = imageList2;
            }
            
            this.toolStripLabel2.Text = "小图标";
        }

        private void 列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListView LI = (ListView)tabControl2.SelectedTab.Controls[0];
            LI.LargeImageList = null;
            LI.SmallImageList = null;
            LI.View = View.Tile;
            this.toolStripLabel2.Text = "列表";
        }


        public void ZT(int a) {
            foreach (TabPage tab in tabControl2.TabPages)
            {

                ModelRoomType mr = (ModelRoomType)tab.Tag;
                Model_Tables mt = new Model_Tables();
                mt.RTID = mr.RTID;
                mt.TableState =a;
                DataTable dr = BBL_RoomType_Tables.CXCanZuo(mt);
                ListView lv = (ListView)tab.Controls[0];
                lv.Items.Clear();

                foreach (DataRow item in dr.Rows)
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = item["TableName"].ToString();
                    int TableState = int.Parse(item["TableState"].ToString());

                    li.ImageIndex = TableState;
                    li.Tag = item;
                    lv.Items.Add(li);

                }
            }
        }

        private void 显示可用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ZT(0);
            toolStripLabel1.Text = "可用";
        }

        private void 显示占用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ZT(1);
            toolStripLabel1.Text = "占用";
        }

        private void 显示停用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ZT(3);
            toolStripLabel1.Text = "停用";
        }

        private void 显示预定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ZT(2);
            toolStripLabel1.Text = "预定";
        }

        private void 显示全部ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Zz();
            toolStripLabel1.Text = "全部";
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            TabPage tp = tabControl2.SelectedTab;
           ListView LI= (ListView)tp.Controls[0];

           try
           {
               if (LI.SelectedItems[0].ImageIndex == 0)
               {
                   顾客开单 k = new 顾客开单(tp, this, p);
                   k.ShowDialog();
               }
               else
               {
                   MessageBox.Show("请选中可用餐桌");
               }
           }
           catch (Exception)
           {

               MessageBox.Show("请选中餐桌，进行操作");
           }
               
         
          
           
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {
            //获取选项卡的第一个控件
            ListView li = this.tabControl2.SelectedTab.Controls[0] as ListView;
            if (li.SelectedItems.Count == 0)
            {
                return;
            }
            DataRow da = li.SelectedItems[0].Tag as DataRow;
            int TableState = Convert.ToInt32(da["TableState"].ToString());
            if (TableState == 0)
            {
                this.客户开单ToolStripMenuItem.Enabled = true;
                this.宾客结账ToolStripMenuItem.Enabled = false;
                this.增加消费ToolStripMenuItem.Enabled = false;
                this.餐桌状态ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Text = "当前状态：可用";
                this.改为可用ToolStripMenuItem.Enabled = false;
                this.改为占用ToolStripMenuItem.Enabled = false;
                this.改为预订ToolStripMenuItem.Enabled = true;
                this.改为停用ToolStripMenuItem.Enabled = true;

            }
            if (TableState == 1)
            {
                this.客户开单ToolStripMenuItem.Enabled = false;
                this.宾客结账ToolStripMenuItem.Enabled = true;
                this.增加消费ToolStripMenuItem.Enabled = true;
                this.餐桌状态ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Text = "当前状态：占用";
                this.改为可用ToolStripMenuItem.Enabled = false;
                this.改为占用ToolStripMenuItem.Enabled = false;
                this.改为预订ToolStripMenuItem.Enabled = false;
                this.改为停用ToolStripMenuItem.Enabled = false;

            }
            if (TableState == 2)
            {
                this.客户开单ToolStripMenuItem.Enabled = false;
                this.宾客结账ToolStripMenuItem.Enabled = true;
                this.增加消费ToolStripMenuItem.Enabled = true;
                this.餐桌状态ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Text = "当前状态：预订";
                this.改为可用ToolStripMenuItem.Enabled = false;
                this.改为占用ToolStripMenuItem.Enabled = false;
                this.改为预订ToolStripMenuItem.Enabled = false;
                this.改为停用ToolStripMenuItem.Enabled = false;
            }
            if (TableState == 3)
            {
                this.客户开单ToolStripMenuItem.Enabled = false;
                this.宾客结账ToolStripMenuItem.Enabled = false;
                this.增加消费ToolStripMenuItem.Enabled = false;
                this.餐桌状态ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Enabled = true;
                this.当前状态可用ToolStripMenuItem.Text = "当前状态：停用";
                this.改为可用ToolStripMenuItem.Enabled = true;
                this.改为占用ToolStripMenuItem.Enabled = false;
                this.改为预订ToolStripMenuItem.Enabled = false;
                this.改为停用ToolStripMenuItem.Enabled = false;
            }
        }

        private void 改为可用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获取当前选择的选项卡的第一个控件
            ListView li = this.tabControl2.SelectedTab.Controls[0] as ListView;
            //当前选中的隐藏值
            DataRow da = li.SelectedItems[0].Tag as DataRow;
            //实例化对象
            Tables tab = new Tables();
            //传值
            tab.tableID = int.Parse(da["TableID"].ToString());
            tab.TableState = 0;
            int count = 主界面BLL.TableState(tab);
            if (count > 0)
            {
                Zz();
            }
            else
            {
                MessageBox.Show("失败");
            }
        }

        private void 改为预订ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获取当前选择的选项卡的第一个控件
            ListView li = this.tabControl2.SelectedTab.Controls[0] as ListView;
            //当前选中的隐藏值
            DataRow da = li.SelectedItems[0].Tag as DataRow;
            //实例化对象
            Tables tab = new Tables();
            //传值
            tab.tableID = int.Parse(da["TableID"].ToString());
            tab.TableState = 2;
            int count = 主界面BLL.TableState(tab);
            if (count > 0)
            {
                Zz();
            }
            else
            {
                MessageBox.Show("失败");
            }
        }

        private void 改为停用ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //获取当前选择的选项卡的第一个控件
            ListView li = this.tabControl2.SelectedTab.Controls[0] as ListView;
            //当前选中的隐藏值
            DataRow da = li.SelectedItems[0].Tag as DataRow;
            //实例化对象
            Tables tab = new Tables();
            //传值
            tab.tableID = int.Parse(da["TableID"].ToString());
            tab.TableState = 3;
            int count = 主界面BLL.TableState(tab);
            if (count > 0)
            {
                Zz();
            }
            else
            {
                MessageBox.Show("失败");
            }
        }

       

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            房间和餐台管理 f = new 房间和餐台管理(this);
            f.ShowDialog();
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            商品设置 a = new 商品设置();
            a.Show();
        }

        private void toolStripSplitButton2_ButtonClick(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            商品消费查询 f = new 商品消费查询();
            f.Show();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            FrmHuiyuanguanli f = new FrmHuiyuanguanli();
            f.Show();
        }

        private void 客户开单ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tp = tabControl2.SelectedTab;
            ListView LI = (ListView)tp.Controls[0];

            try
            {
                if (LI.SelectedItems[0].ImageIndex == 0)
                {
                    顾客开单 k = new 顾客开单(tp, this,p);
                    k.ShowDialog();
                }
                else
                {
                    MessageBox.Show("请选中可用餐桌");
                }
            }
            catch (Exception)
            {

                MessageBox.Show("请选择你要开单的桌子");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            TabPage tp = tabControl2.SelectedTab;
            ListView LI = (ListView)tp.Controls[0];

            try
            {
                if (LI.SelectedItems[0].ImageIndex == 1)
                {
                    Model_ConsumerBill m = new Model_ConsumerBill();
                    m.CBID = CBID;
                    m.CBDiscount = CBDiscount;
                    m.VipID = VipID;
                    // MessageBox.Show(CBID);
                    添加消费 k = new 添加消费(tp, this, m);
                    k.ShowDialog();
                }
                else
                {
                    MessageBox.Show("请选中正在使用的餐桌进行追加消费");
                }
            }
            catch (Exception)
            {

                MessageBox.Show("请选中餐桌，进行操作");
            }
               
          
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            //跳转
            账单查询 f = new 账单查询();
            f.Show();
        }

    
        }
    }

